package sz.szsmk.citizencard.mvp.dzsmkpresenter;


import com.google.gson.Gson;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.FaqResponse;
import sz.szsmk.citizencard.model.network.dzbean.res.Qa;
import sz.szsmk.citizencard.mvp.FAQMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;

/**
 * Created by yangzhengwen on 2017/10/5.
 */

public class FAQPresenter extends AppBasePresenter<FAQMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;
    private ArrayList<Qa> list = new ArrayList<>();

    @Inject
    public FAQPresenter() {
        getComponent().inject(this);
    }

    public void getFAQ() {
        checkViewAttached();
        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();
        mNetworkDateSource.getQa(commonRequest, new Action1<FaqResponse>() {
            @Override
            public void call(FaqResponse faqResponse) {
                String code = faqResponse.getCode();
                if (code.equals("0000")) {
                    List<FaqResponse.DataBean> data = faqResponse.getData();
                    if (data == null) {
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        FaqResponse.DataBean dataBean = data.get(i);
                        String json = new Gson().toJson(dataBean);
                        Qa qa = new Gson().fromJson(json, Qa.class);
                        list.add(qa);
                    }
                    getMvpView().onGetQASuccess(list);
                } else {
                    getMvpView().onGetQAFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
